package com.ama.controller;

import com.ama.dao.EstateDao;
import com.ama.pojo.Estate;
import com.ama.utils.NumberUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class EstateManageController {
    @Autowired
    EstateDao estateDao;

    @RequestMapping("/addEstate")
    public String estateAdd(HttpServletRequest request) {
        Long estateId = Long.valueOf(request.getParameter("estateId"));
        Long propertyId = Long.valueOf(request.getParameter("propertyId"));
        String name = request.getParameter("estateName");
        String estateAddress = request.getParameter("estateAddress");
        Double estateArea = Double.parseDouble(request.getParameter("estateArea"));
        Integer estate_state = Integer.parseInt(request.getParameter("estate_state"));
        Integer estate_open = Integer.parseInt(request.getParameter("estate_open"));
        String estateInfo = request.getParameter("estateInfo");
        Estate estate = new Estate();
        estate.setEstateId(estateId);
        estate.setPropertyId(propertyId);
        estate.setEstateName(name);
        estate.setEstateAddress(estateAddress);
        estate.setEstateArea(NumberUtils.round(estateArea, 2));
        estate.setEstateState(estate_state);
        estate.setEstateOpen(estate_open);
        estate.setEstateInfo(estateInfo);
        estateDao.insert(estate);
        return "redirect:/success/addBuildManage_suc.jsp";
    }

    @RequestMapping("/openEstate")
    public String openEstate(HttpServletRequest request) {
        Long estateId = Long.valueOf(request.getParameter("estate"));
        Estate estate = estateDao.selectById(estateId);
        estate.setEstateOpen(1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("estate_id", estateId);
        estateDao.update(estate, wrapper);
        return "redirect:/pro_page/p_estatemanage.jsp";

    }

    @RequestMapping("/banEstate")
    public String banEstate(HttpServletRequest request) {
        Long estateId = Long.valueOf(request.getParameter("estate"));
        Estate estate = estateDao.selectById(estateId);
        estate.setEstateOpen(-1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("estate_id", estateId);
        estateDao.update(estate, wrapper);
        return "redirect:/pro_page/p_estatemanage.jsp";

    }

    @RequestMapping("/delEstate")
    public String delEstate(HttpServletRequest request) {
        Long estateId = Long.valueOf(request.getParameter("estate"));
        estateDao.deleteById(estateId);
        return "redirect:/pro_page/p_estatemanage.jsp";
    }

}
